﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using BLL;

namespace FlowMvc.Controllers
{
    public class FlowController : Controller
    {
        SysFlowBLL bll = new SysFlowBLL();

        // GET: Flow
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取流程信息
        /// </summary>
        /// <returns></returns>
        public ActionResult AjaxFlowData()
        {
            int pageIndex = Request.Form["offset"] == null ? 1 : int.Parse(Request.Form["offset"]);
            int pageSize = Request.Form["limit"] == null ? 10 : int.Parse(Request.Form["limit"]);
            if (pageIndex != 0)
            {// 获取页数
                pageIndex = pageIndex / pageSize;
            }
            pageIndex += 1;
            int total = 0;
            List<SysFLow> getData = null;
            //using (CFA_OAEntities db = new CFA_OAEntities())
            //{

            //    try
            //    {
            //        getData = db.SysFLow.AsNoTracking().OrderByDescending(i => i.ID).Select(i => i).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            //        total = db.SysFLow.Count();
            //    }
            //    catch (Exception ex)
            //    {
            //        return Json(new { total = 0, rows = new List<object>() }, JsonRequestBehavior.AllowGet);
            //    }
            //}
            getData = bll.GetList(pageIndex, pageSize, ref total);
            if (getData != null)
            {
                var list = (from i in getData.ToList()
                            select new
                            {
                                ID = i.ID,
                                FlowID = i.Code,
                                FlowName = i.Name,
                                Remark = i.Remark
                            }).ToList();
                return Json(new { total = total, rows = list }, JsonRequestBehavior.AllowGet);

            }
            return Json(new { total = total, rows = new object() }, JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        /// 保存流程信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AjaxSave(SysFlows entity)
        {

            //using (CFA_OAEntities db = new CFA_OAEntities())
            //{
            //    int result = 0;
            //    if (entity.id != 0)
            //    {
            //        SysFLow model = db.SysFLow.Where(i => i.ID == entity.id).FirstOrDefault();
            //        if (model != null)
            //        {
            //            model.Code = entity.form_id;
            //            model.Name = entity.flow_name;
            //            model.Remark = entity.remark;
            //        }
            //    }
            //    else
            //    {
            //        SysFLow flow = new SysFLow();
            //        flow.Code = entity.form_id;
            //        flow.Name = entity.flow_name;
            //        flow.Remark = entity.remark;
            //        db.SysFLow.Add(flow);
            //    }
            //    result = db.SaveChanges();
            //    if (result > 0)
            //    {
            //        return Json(new { statu = 1 }, JsonRequestBehavior.AllowGet);
            //    }
            //}
            return Json(new { statu = 0 }, JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        ///  导入OA
        /// </summary>
        /// <returns></returns>
        public ActionResult AjaxImprot()
        {
            try
            {
                string flowID = Request.Params["FlowID"];
                if (!string.IsNullOrWhiteSpace(flowID))
                {
                    if(bll.ImprotOA(Convert.ToInt32(flowID))>0){
                        return Json(new { statu =1 }, JsonRequestBehavior.AllowGet);
                    }
                }
            }
            catch (Exception)
            {
                return Json(new { statu = 0 }, JsonRequestBehavior.AllowGet);
            }
            return Json(new { statu = 0 }, JsonRequestBehavior.AllowGet);

        }
    }
}